<template>
  <view>
    <!-- 选择收货地址区域 -->
    <view class="address-box" v-if="JSON.stringify(addressList)==='{}'">
      <button type="primary" size="mini" class="addressButton" @click="addressClick">请选择收货地址+</button>
    </view>
    <!-- 渲染收货信息的盒子 -->
    <view class="address-info-box" v-else @click="addressClick">
      <view class="row1">
        <view class="row1-left-text">收货人:<text>{{addressList.userName}}</text></view>
        <view class="row1-right-text">
          电话：<text>{{addressList.telNumber}}</text>
          <uni-icons type="forward" size="16"></uni-icons>
        </view>
      </view>
      <!-- 收货地址 -->
      <view class="row2">
        <view class="row2-text">收货地址：</view>
        <view class="row2-dz">{{addresser}}</view>
      </view>
    </view>
    <!-- 底部边框线 -->
    <image src="/static/cart_border@2x.png" class="address-img"></image>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    name: "my-address",
    data() {
      return {
        //收货地址
        //addressList: {}
      };
    },
    methods: {
      //把 m_userInfo 模块中的 updateaddressList 函数映射到当前组件
      ...mapMutations('m_userInfo',['updateaddressList']),
      //获取用户收货地址
      async addressClick() {
        // 1. 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
        //    返回值是一个数组：第 1 项为错误对象；第 2 项为成功之后的收货地址对象
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        if (err === null && succ.errMsg === 'chooseAddress:ok')
         //调用 Store 中提供的 updateaddressList 方法，将 addressList 保存到 Store 里面
        this.updateaddressList(succ)
      }
    },
    computed: {
      //把 m_userInfo 模块中的 addressList 对象映射当前组件中使用，代替 data 中 addressList 对象
      ...mapState('m_userInfo',['addressList']),
      // 收货详细地址的计算属性
      ...mapGetters('m_userInfo',['addresser'])
    }
  }
</script>

<style lang="scss">
  //选择收货地址
  .address-box {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;

    .addressButton {}
  }

  //收货信息
  .address-info-box {
    padding: 5px 5px;
    flex-direction: column;
    justify-content: center;
    font-size: 13px;

    .row1 {
      display: flex;
      justify-content: space-between;

      .row1-right-text {
        margin-left: 5px;
      }
    }

    .row2 {
      display: flex;
      align-items: center;
      margin-top: 10px;

      .row2-text {
        white-space: nowrap;
      }
    }
  }

  .address-img {
    display: block;
    width: 100%;
    height: 5px;
  }
</style>
